import pandas as pd

# 读取筛选后的数据集
df_Master = pd.read_csv('F:\\数据\\df_Master.csv', encoding='gb18030')

# 定义城市和省份特征
city_feature = ['UserInfo_2', 'UserInfo_20', 'UserInfo_4', 'UserInfo_8']
province_feature = ['UserInfo_7', 'UserInfo_19']

# 城市特征去重统计
print("城市特征去重计数：")
for col in city_feature:
    print(f"{col}: {df_Master[col].nunique()}")

# 省份特征去重统计
print("\n省份特征去重计数：")
for col in province_feature:
    print(f"{col}: {df_Master[col].nunique()}")

# 清洗城市特征：去除“市”字
df_Master['UserInfo_8'] = [a[:-1] if a.find('市') != -1 else a for a in df_Master['UserInfo_8']]
print(f"清洗后UserInfo_8去重计数: {df_Master['UserInfo_8'].nunique()}")

# 定义违约率计算函数
def get_badrate(df, col):
    group = df.groupby(col)
    return pd.DataFrame({
        'total': group.target.count(),
        'bad': group.target.sum(),
        'badrate': round(group.target.sum()/group.target.count(), 4)*100
    }).sort_values('badrate', ascending=False)

# 计算户籍省份违约率
province_original = get_badrate(df_Master, 'UserInfo_19')

# 计算居住省份违约率
province_current = get_badrate(df_Master, 'UserInfo_7')

# 生成省份二值特征（前5名）
for province in province_original.head(5).index:
    df_Master[f'is_{province}_original'] = df_Master['UserInfo_19'].apply(lambda x: 1 if x==province else 0)

for province in province_current.head(5).index:
    df_Master[f'is_{province}_current'] = df_Master['UserInfo_7'].apply(lambda x: 1 if x==province else 0)

# 衍生户籍与居住省份一致性特征
UserInfo_19_change = []
for i in df_Master['UserInfo_19']:
    if i in ('内蒙古自治区', '黑龙江省'):
        j = i[:3]
    else:
        j = i[:2]
    UserInfo_19_change.append(j)

df_Master['is_same_province'] = [1 if i==j else 0 for i,j in zip(df_Master['UserInfo_7'], UserInfo_19_change)]


#1.地理特征清洗  去除‘市’后缀
#2.计算关键特征的违约率 bad rate
#3.创建高违约省份的二值特征
#4.构建户籍与居住地一致性特征